<?php
class DownAction extends ThinkAPK_Action_Web {
	
	function index(){
		//echo fopen('http://down.mumayi.com/1', 'r');
		//print_r($this->getFileSize('http://down.mumayi.com/1'));
//		print_r(basename('http://apk.mumayi.com/6687/nongfudazhanwaixingrenRednecks_VSAliens_mumayi_2993b.apk'));
//		$row = M('Temp')->where('is_dwon=0')->field('id,file_url')->order('id DESC')->limit(100)->findAll();
//		print_r(M('Temp')->getLastSql());

		echo strtotime('2011-01-01 00:00:00');
		$this->display();
	}
	
	function ajax1(){
		$row = M('Temp')->where('is_down=1')->order('id DESC')->find();
		if(!$row){
			$this->error('处理完成！');
		}else{
			$d = fopen('http://down.mumayi.com/'.$row['target_id'], 'r');
			$headerstr = implode("||", $http_response_header);
			preg_match("/Location:(.*)\|\|/isU", $headerstr, $url);
			$apkurl = '';
			if(isset($url[1]) && !empty($url[1]) && !strpos($url[1], '404.html')){
				$apkurl = $url[1];
			}else{
				$this->success('未找到地址：'.$row['id']);
			}
			if($apkurl){
				M('Temp')->where('id='.$row['id'])->save(array('is_down'=>0, 'file_url'=>$apkurl));
				
				$this->success('ID为'.$row['id'].'处理完成：'.$apkurl);
			}
			$this->success('ID为'.$row['id'].'处理失败');
		}
	}
	
	function ajax2(){
		$row = M('Temp')->where('is_down=0')->field('id,file_url')->order('id DESC')->limit(100)->findAll();
		if($row){
			foreach($row as $res){
				$name = basename($res['file_url']);
				$name = str_replace('_mumayi_', '_thinkapk.com_', $name);
				$name = 'http://app1.thinkapk.com/down/'.$name;
				M('Temp')->where('id='.$res['id'])->save(array('file_url'=>$name, 'is_down'=>1));
			}
			$this->success('OK');
		}else{
			$this->error('false');
		}
	}
	
	function ajax3(){
		$row = M('Temp')->where('is_down=1')->field('id,icon_url')->order('id DESC')->limit(100)->findAll();
		if($row){
			foreach($row as $res){
				$ext = $this->getext($res['icon_url']);
				M('Temp')->where('id='.$res['id'])->save(array('icon_url'=>'http://i.thinkapk.com/icons/'.$res['id'].'.'.$ext, 'is_down'=>0));
			}
		}
		$this->success('ok'.time());
	}
	
	function ajax(){
		$row = M('Temp')->where('is_down=0')->order('id DESC')->limit(100)->findALl();
		if($row){
			foreach ($row as $res){
				$data['goods_name'] = $res['title'];
				$data['goods_brief'] = $res['berif'];
				$data['goods_desc'] = $res['berif'];
				$data['cat_id'] = $res['cat_id'];
				$data['click_count'] = mt_rand(30, 1000);
				$data['old_id'] = $res['target_id'];
				$data['add_time'] = mt_rand(1293811200, time());
				$data['last_update'] =  $data['add_time'];
				$data['goods_icon'] = $res['new_icon'];
				$data['short_desc'] = $res['short_desc'];
				$data['recommend'] = mt_rand(1, 5);
				$data['last_down'] = mt_rand(1293831200, time());
				M('Goods')->add($data);
				$id = M('Goods')->getLastInsID();
				if($id){
					//下载文件
					$downdata['goods_id'] = $id;
					$downdata['app_url'] = $res['file_url'];
					M('App')->add($downdata);
					//
					$gcount['goods_id'] = $id;
					$gcount['cat_id'] = $res['cat_id'];
					$gcount['hits'] = mt_rand(86, 2000);
					$gcount['down_total'] =  mt_rand(0, 2000);
					$gcount['good'] = mt_rand(0, 200);
					$gcount['bad'] = mt_rand(0 ,10);
					M('GoodsCount')->add($gcount);
					//
					M('Temp')->where('id='.$res['id'])->save(array('is_down'=>1));
				}
			}
			$this->success('ok'.time());
		}else{
			$this->error('完成');
		}
		
	}
	
	function getext($f){
		$ext = substr($f, -3);
		return $ext;
	}
}
?>